package com.wz.project.api.barcode.service;

import com.wz.framework.aspectj.lang.annotation.DataSource;
import com.wz.framework.aspectj.lang.enums.DataSourceType;
import com.wz.project.api.barcode.domain.Barcode;
import com.wz.project.api.barcode.domain.BarcodeFwm;
import com.wz.project.api.barcode.domain.BarcodeInfo;
import com.wz.project.api.barcode.domain.MatnrInfo;
import com.wz.project.api.barcode.mapper.BarcodeFwmMapper;
import com.wz.project.api.barcode.mapper.BarcodeInfoMapper;
import com.wz.project.api.barcode.mapper.MatnrMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;
import java.util.List;

@Service
@DataSource(value = DataSourceType.SLAVE)
public class BarcodeServiceImpl implements BarcodeService{

    @Autowired
    BarcodeInfoMapper barcodeInfoMapper;
    @Autowired
    MatnrMapper matnrMapper;
    @Autowired
    BarcodeFwmMapper barcodeFwmMapper;

    @Autowired
    private EntityManager entityManager;
    @Override
    public BarcodeInfo selectBarcodeById(String fbarcode) {
        return barcodeInfoMapper.selectBarcodeInfoById(fbarcode);
    }

    @Override
    public List<MatnrInfo> selectMatnrInfo(MatnrInfo matnrInfo) {
        return matnrMapper.selectMatnrList(matnrInfo);
    }

    @Override
    public List<BarcodeFwm> selectBarcodeFwmInfo(BarcodeFwm barcodeFwm) {
        return barcodeFwmMapper.selectBarcodeFwmList(barcodeFwm);
    }

    @Override
    public Barcode selectBarcodeByIdHs(String fbarcode) {
        Barcode barcode = null;
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("sp_tm_barcode_info");
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("barcode", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("FBARCODE", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("MATNR", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("FNUM", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("FUNIT", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("FFLAG", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("CREATETIME", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("AUFNR", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("PURCHASENO", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("UCODE", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("EBELP", String.class, ParameterMode.OUT);

        parameter.setParameter("barcode", fbarcode);
        //执行
        storedProcedureQuery.execute();
        //获取返回结果
        String result = (String) storedProcedureQuery.getOutputParameterValue("FBARCODE");
        if(result !=null ){
            barcode = new Barcode();
            barcode.setFbarcode(result);
            barcode.setFbarcode( (String) storedProcedureQuery.getOutputParameterValue("FBARCODE"));
            barcode.setMatnr( (String) storedProcedureQuery.getOutputParameterValue("MATNR"));
            barcode.setFnum( (String) storedProcedureQuery.getOutputParameterValue("FNUM"));
            barcode.setFunit( (String) storedProcedureQuery.getOutputParameterValue("FUNIT"));
            barcode.setFflag( (String) storedProcedureQuery.getOutputParameterValue("FFLAG"));
            barcode.setCreatetime( (String) storedProcedureQuery.getOutputParameterValue("CREATETIME"));
            barcode.setAufnr( (String) storedProcedureQuery.getOutputParameterValue("AUFNR"));
            barcode.setPurchaseno( (String) storedProcedureQuery.getOutputParameterValue("PURCHASENO"));
            barcode.setUcode( (String) storedProcedureQuery.getOutputParameterValue("UCODE"));
            barcode.setEbelp( (String) storedProcedureQuery.getOutputParameterValue("EBELP"));
        }

        return barcode;
    }
}
